import * as glMatrix from 'gl-matrix';

console.log("sksjdajdajsdjsad");
window.onload = () => {
    var el = document.getElementById('content');
    setInterval(() => {
        for (var i = 0; i < 10000; i++) {
            glMatrix.mat4.fromRotationTranslationScale(glMatrix.mat4.create(), glMatrix.quat.create(), glMatrix.vec3.fromValues(1, 2, 3), glMatrix.vec3.fromValues(1, 2, 3));
            Matrix.makeTransformRTS(new Vector3(1, 2, 3), new Vector3(1, 2, 3), new Quaternion(), new Matrix());
        }
    }, 1);
    //greeter.start();
};
export class test {
    constructor(){
        console.log("111111111111111111111111111111");
    }
}
new test();
export class Vector3 {
    constructor(x: number = 0, y: number = 0, z: number = 0) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    x: number;
    y: number;
    z: number;
    toString(): string {
        return this.x + "," + this.y + "," + this.z;
    }
}
class Quaternion {
    constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 1) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
    }
    x: number;
    y: number;
    z: number;
    w: number;
    toString(): string {
        return this.x + "," + this.y + "," + this.z + "," + this.w;
    }
    static toMatrix(src: Quaternion, out: Matrix) {
        var xy2: number = 2.0 * src.x * src.y, xz2: number = 2.0 * src.x * src.z, xw2: number = 2.0 * src.x * src.w;
        var yz2: number = 2.0 * src.y * src.z, yw2: number = 2.0 * src.y * src.w, zw2: number = 2.0 * src.z * src.w;
        var xx: number = src.x * src.x, yy: number = src.y * src.y, zz: number = src.z * src.z, ww: number = src.w * src.w;

        out.rawData[0] = xx - yy - zz + ww;
        out.rawData[4] = xy2 - zw2;
        out.rawData[8] = xz2 + yw2;
        out.rawData[12] = 0;
        out.rawData[1] = xy2 + zw2;
        out.rawData[5] = -xx + yy - zz + ww;
        out.rawData[9] = yz2 - xw2;
        out.rawData[13] = 0;
        out.rawData[2] = xz2 - yw2;
        out.rawData[6] = yz2 + xw2;
        out.rawData[10] = -xx - yy + zz + ww;
        out.rawData[14] = 0;
        out.rawData[3] = 0.0;
        out.rawData[7] = 0.0;
        out.rawData[11] = 0;
        out.rawData[15] = 1;
    }
}
class Matrix {
    public rawData: Float32Array;
    constructor(datas: Float32Array = null) {
        if (datas) {
            this.rawData = datas;
        }
        else
            this.rawData = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
    }

    toString(): string {
        return "[" + this.rawData[0] + "," + this.rawData[1] + "," + this.rawData[2] + "," + this.rawData[3] + "],"
            + "[" + this.rawData[4] + "," + this.rawData[5] + "," + this.rawData[6] + "," + this.rawData[7] + "],"
            + "[" + this.rawData[8] + "," + this.rawData[9] + "," + this.rawData[10] + "," + this.rawData[11] + "],"
            + "[" + this.rawData[12] + "," + this.rawData[13] + "," + this.rawData[14] + "," + this.rawData[15] + "]";
    }

    static clone(src: Matrix, out: Matrix) {
        for (var i = 0; i < 16; i++) {
            out.rawData[i] = src.rawData[i];
        }
    }

    static identity(out: Matrix) {
        out.rawData.set(
            [
                1.0, 0, 0, 0,
                0, 1.0, 0, 0,
                0, 0, 1.0, 0,
                0, 0, 0, 1.0
            ]
        );
    }
    static toZero(out: Matrix) {
        out.rawData.set(
            [
                0, 0, 0, 0,
                0, 0, 0, 0,
                0, 0, 0, 0,
                0, 0, 0, 1.0
            ]
        );
    }
    static makeScale(xScale: number, yScale: number, zScale: number, out: Matrix): void {

        out.rawData[0] = xScale; out.rawData[1] = 0.0; out.rawData[2] = 0.0; out.rawData[3] = 0.0;
        out.rawData[4] = 0.0; out.rawData[5] = yScale; out.rawData[6] = 0.0; out.rawData[7] = 0.0;
        out.rawData[8] = 0.0; out.rawData[9] = 0.0; out.rawData[10] = zScale; out.rawData[11] = 0.0;
        out.rawData[12] = 0.0; out.rawData[13] = 0.0; out.rawData[14] = 0.0; out.rawData[15] = 1.0;
    }
    static multiply(lhs: Matrix, rhs: Matrix, out: Matrix): void {
        var a00 = lhs.rawData[0], a01 = lhs.rawData[1], a02 = lhs.rawData[2], a03 = lhs.rawData[3];
        var a10 = lhs.rawData[4], a11 = lhs.rawData[5], a12 = lhs.rawData[6], a13 = lhs.rawData[7];
        var a20 = lhs.rawData[8], a21 = lhs.rawData[9], a22 = lhs.rawData[10], a23 = lhs.rawData[11];
        var a30 = lhs.rawData[12], a31 = lhs.rawData[13], a32 = lhs.rawData[14], a33 = lhs.rawData[15];

        var b0 = rhs.rawData[0],
            b1 = rhs.rawData[1],
            b2 = rhs.rawData[2],
            b3 = rhs.rawData[3];

        out.rawData[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
        out.rawData[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
        out.rawData[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
        out.rawData[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;

        b0 = rhs.rawData[4];
        b1 = rhs.rawData[5];
        b2 = rhs.rawData[6];
        b3 = rhs.rawData[7];

        out.rawData[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
        out.rawData[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
        out.rawData[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
        out.rawData[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;

        b0 = rhs.rawData[8];
        b1 = rhs.rawData[9];
        b2 = rhs.rawData[10];
        b3 = rhs.rawData[11];

        out.rawData[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
        out.rawData[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
        out.rawData[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
        out.rawData[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;

        b0 = rhs.rawData[12];
        b1 = rhs.rawData[13];
        b2 = rhs.rawData[14];
        b3 = rhs.rawData[15];

        out.rawData[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
        out.rawData[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
        out.rawData[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
        out.rawData[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
    }
    static makeTransformRTS(pos: Vector3, scale: Vector3, rot: Quaternion, out: Matrix) {
        var matS = pool.new_matrix();
        this.makeScale(scale.x, scale.y, scale.z, matS);
        var matR = pool.new_matrix();
        Quaternion.toMatrix(rot, matR);
        this.multiply(matR, matS, out);

        out.rawData[12] = pos.x;
        out.rawData[13] = pos.y;
        out.rawData[14] = pos.z;
        out.rawData[15] = 1;

        pool.delete_matrix(matS);
        pool.delete_matrix(matR);
    }
}
class pool {
    static collect_all() {

        pool.collect_vector3();
        pool.collect_matrix();
    }

    //for vector3
    private static _vector3_up: Vector3;
    static get vector3_up(): Vector3 {
        if (pool._vector3_up == null) {
            pool._vector3_up = new Vector3(0, 1, 0);
        }
        return pool._vector3_up;
    }
    private static _vector3_right: Vector3;
    static get vector3_right(): Vector3 {
        if (pool._vector3_right == null) {
            pool._vector3_right = new Vector3(1, 0, 0);
        }
        return pool._vector3_right;
    }
    private static _vector3_forward: Vector3;
    static get vector3_forward(): Vector3 {
        if (pool._vector3_forward == null) {
            pool._vector3_forward = new Vector3(0, 0, 1);
        }
        return pool._vector3_forward;
    } n
    private static _vector3_zero: Vector3;
    static get vector3_zero(): Vector3 {
        if (pool._vector3_zero == null) {
            pool._vector3_zero = new Vector3(0, 0, 0);
        }
        return pool._vector3_zero;
    }
    private static _vector3_one: Vector3;
    static get vector3_one(): Vector3 {
        if (pool._vector3_one == null) {
            pool._vector3_one = new Vector3(1, 1, 1);
        }
        return pool._vector3_one;
    }

    private static unused_vector3: Vector3[] = []
    static new_vector3(): Vector3 {
        if (pool.unused_vector3.length > 0) {
            let v = pool.unused_vector3.pop();
            return v;
        }
        else
            return new Vector3();
    }
    static clone_vector3(src: Vector3): Vector3 {
        if (pool.unused_vector3.length > 0) {
            var v = pool.unused_vector3.pop();
            v.x = src.x;
            v.y = src.y;
            v.z = src.z;
            return v;
        }
        else
            return new Vector3(src.x, src.y, src.z);
    }
    static delete_vector3(v: Vector3): void {
        if (v == null) return;
        if (v instanceof Vector3) {
            v.x = v.y = v.z = 0;
            pool.unused_vector3.push(v);
        }
        else
            console.error("kindding me?确定你要回收的是vector3吗？");
    }
    static collect_vector3() {
        pool.unused_vector3.length = 0;//清除未使用的vector 池子
    }

    //for matrix
    private static unused_matrix: Matrix[] = []
    static new_matrix(): Matrix {
        if (pool.unused_matrix.length > 0)
            return pool.unused_matrix.pop();
        else
            return new Matrix();
    }
    static clone_matrix(src: Matrix): Matrix {
        var v: Matrix = pool.new_matrix();
        for (var i = 0; i < 16; i++)
            v.rawData[i] = src.rawData[i];
        return v;
    }
    static delete_matrix(v: Matrix): void {
        if (v == null) return;
        if (v instanceof Matrix) {
            v.rawData[0] = 1;
            v.rawData[1] = 0;
            v.rawData[2] = 0;
            v.rawData[3] = 0;

            v.rawData[4] = 0;
            v.rawData[5] = 1;
            v.rawData[6] = 0;
            v.rawData[7] = 0;

            v.rawData[8] = 0;
            v.rawData[9] = 0;
            v.rawData[10] = 1;
            v.rawData[11] = 0;

            v.rawData[12] = 0;
            v.rawData[13] = 0;
            v.rawData[14] = 0;
            v.rawData[15] = 1;
            pool.unused_matrix.push(v);
        }
        else
            console.error("kindding me?确定你要回收的是matrix吗？");
    }
    static collect_matrix() {
        pool.unused_matrix.length = 0;//清除未使用的池子
    }

}



console.log("##########################");